/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Administrator
 */
public class UsernameValidator implements Validator {

    public void validate(FacesContext context, UIComponent component, Object user) throws ValidatorException {

        if (!(user instanceof String)) {
            FacesMessage message = new FacesMessage();
            message.setSummary("* Errore irreversibile");
            throw new ValidatorException(message);
        }
        String username = (String) user;
        //verifico che non sia vuoto
        if (username.length() == 0) {
            FacesMessage message = new FacesMessage();
            message.setSummary("* Devi inserire un nome per la Username");
            throw new ValidatorException(message);
        }
        //verifico che sia superiore a 3
        if (username.length() < 2) {
            FacesMessage message = new FacesMessage();
            message.setSummary("* La Username deve avere almeno 3 caratteri");
            throw new ValidatorException(message);
        }
    }
}
